package com.ruoyi.comparison.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.comparison.domain.TaskResult;
import com.ruoyi.comparison.domain.TaskResultDetail;
import com.ruoyi.comparison.domain.bo.TaskResultDetailBo;
import com.ruoyi.comparison.domain.vo.TaskResultDetailVo;
import com.ruoyi.comparison.mapper.TaskResultDetailMapper;
import com.ruoyi.comparison.service.ITaskResultDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 比对结果详情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@RequiredArgsConstructor
@Service
public class TaskResultDetailServiceImpl implements ITaskResultDetailService {

    private final TaskResultDetailMapper baseMapper;

    /**
     * 查询比对结果详情
     */
    @Override
    public TaskResultDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询比对结果详情列表
     */
    @Override
    public TableDataInfo<TaskResultDetailVo> queryPageList(TaskResultDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskResultDetail> lqw = buildQueryWrapper(bo);
        lqw.eq(TaskResultDetail::getResultId,bo.getResultId());
        lqw.orderByAsc(TaskResultDetail::getLeftNum);
        Page<TaskResultDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询比对结果详情列表
     */
    @Override
    public List<TaskResultDetailVo> queryList(TaskResultDetailBo bo) {
        LambdaQueryWrapper<TaskResultDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaskResultDetail> buildQueryWrapper(TaskResultDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskResultDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getResultId() != null, TaskResultDetail::getResultId, bo.getResultId());
        lqw.like(StringUtils.isNotBlank(bo.getLeftName()), TaskResultDetail::getLeftName, bo.getLeftName());
        lqw.eq(bo.getLeftNum() != null, TaskResultDetail::getLeftNum, bo.getLeftNum());
        lqw.like(StringUtils.isNotBlank(bo.getRightName()), TaskResultDetail::getRightName, bo.getRightName());
        lqw.eq(bo.getRightNum() != null, TaskResultDetail::getRightNum, bo.getRightNum());
        lqw.eq(bo.getPreNum() != null, TaskResultDetail::getPreNum, bo.getPreNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPreWord()), TaskResultDetail::getPreWord, bo.getPreWord());
        return lqw;
    }

    /**
     * 新增比对结果详情
     */
    @Override
    public Boolean insertByBo(TaskResultDetailBo bo) {
        TaskResultDetail add = BeanUtil.toBean(bo, TaskResultDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改比对结果详情
     */
    @Override
    public Boolean updateByBo(TaskResultDetailBo bo) {
        TaskResultDetail update = BeanUtil.toBean(bo, TaskResultDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskResultDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除比对结果详情
     */
    @Override
    public Boolean deleteWithValidByIds(List<Long> ids) {
        LambdaQueryWrapper<TaskResultDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TaskResultDetail::getResultId,ids);
        return baseMapper.delete(wrapper) > 0;
    }

    @Override
    public List<TaskResultDetail> listByResultId(Long id) {
        LambdaQueryWrapper<TaskResultDetail> w = new LambdaQueryWrapper<>();
        w.eq(TaskResultDetail::getResultId,id);
        List<TaskResultDetail> list = this.baseMapper.selectList(w);
        return list;
    }

    @Override
    public TaskResultDetailVo findById(String dup) {
        LambdaQueryWrapper<TaskResultDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskResultDetail::getPreWord,dup);
        return this.baseMapper.selectVoOne(wrapper);
    }

    @Override
    public void insertByBoList(List<TaskResultDetail> collect) {
        this.baseMapper.insertBatch(collect);
    }
}
